package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.IYddUserDataService;
import com.itheima.ydd.service.IYddUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_user")
public class YddUserController {
    @Autowired
    private IYddUserService userService;

    @Autowired
    private IYddUserDataService userDataService;
    //分页查询
    @GetMapping("/list")
    public ResultData list(Integer sex,String username, String mobile,int page,int limit) {
        Page<YddUser> pageInfo = new Page<>(page,limit);
        LambdaQueryWrapper<YddUser>wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(sex != null, YddUser::getSex,sex);
        wrapper.like(StringUtils.isNotEmpty(username),YddUser::getUsername,username);
        wrapper.eq(StringUtils.isNotEmpty(mobile), YddUser::getMobile,mobile);
        wrapper.orderByDesc(YddUser::getId);
        userService.page(pageInfo,wrapper);

        //将数据封装为pageResult
        PageResult pageResult = PageResult.getResult(pageInfo);

        return ResultData.ok("",pageResult);
    }
    //数据回显
    @GetMapping("/rowInfo")
    public ResultData rowInfo(YddUser yddUser) {
        YddUser user = userService.getById(yddUser.getId());
        //查询时不返回密码
        user.setPassword("");
        return ResultData.ok("",user);
    }
    //修改
    @PostMapping("/update")
    public ResultData update(@RequestBody YddUser yddUser) {

        //修改密码
        if (StringUtils.isNotEmpty(yddUser.getPassword())) {
            String password = DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes());
            yddUser.setPassword(password);
        }else{
            //如果传回的密码为空,则修改密码为原密码
            YddUser user = userService.getById(yddUser.getId());
            yddUser.setPassword(user.getPassword());
        }

        yddUser.setIcon("http://localhost/undefined"+yddUser.getIcon());

        userService.updateById(yddUser);
        return ResultData.ok("","");

    }
    //删除
    @GetMapping("/del")
    public ResultData delete(YddUser yddUser) {

        YddUser user = userService.getById(yddUser.getId());
        if ("1".equals(user.getDefaultData())) {
            throw new BusinessException("系统默认数据无法删除!");
        }

        userService.removeById(yddUser.getId());
        return ResultData.ok("","");
    }
    //增加
    @PostMapping("/add")
    public ResultData save(@RequestBody YddUser yddUser) {

        String password = DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes());
        yddUser.setPassword(password);

        yddUser.setIcon("http://localhost/undefined"+yddUser.getIcon());
        userService.save(yddUser);
        return ResultData.ok("","");

    }


}
